﻿using HandmadeShop.Application.Customs;
using HandmadeShop.Application.Models;
using HandmadeShop.BusinessLogic;
using HandmadeShop.BusinessLogic.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace HandmadeShop.Application.Controllers
{
    public class ProductController : BaseController
    {
        private readonly string ImageFolder = "~/Images/Products/";

        private HandmadeShop.Application.Models.BusinessLogic.ProductBL bl;
        private ProductBL productBL;

        public ProductController()
        {
            Init(false);
        }

        private void Init(bool user)
        {
            if (user)
                this.productBL = new ProductBL(Context.Instance.CurrentContext.Id);
            else
                this.productBL = new ProductBL();

            if (this.productBL != null)
            {
                this.bl = new Models.BusinessLogic.ProductBL(this.productBL);
            }
        }

        private void SetProductTypeView() 
        {
            List<SelectListItem> listProductTypes = this.bl.GetAllProductType();
            ViewBag.ProductType = listProductTypes;
        }

        public ActionResult ViewByType(string code)
        {
            Guid id = this.productBL.GetTypeByCode(code);
            ViewBag.Products = this.bl.GetProductsByType(id);

            ViewBag.ProductTypes = this.bl.GetMenuItems();
            ViewBag.MenuActiveItem = code;

            return View();
        }

        //[AuthorizeUser(AccessLevel = "Admin")]
        public ActionResult Create()
        {
            Init(true);
            this.SetProductTypeView();

            return View();
        }

        [HttpPost]
        //[AuthorizeUser(AccessLevel = "Admin")]
        public ActionResult Create(CreateProductModel model, string ProductType, string isCreateNewType, HttpPostedFileBase file)
        {
            Init(true);
            StringBuilder sb = new StringBuilder();
            foreach (char c in model.Price)
            {
                if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
                {
                    sb.Append(c);
                }
            }

            string price = sb.ToString();
            price = price.Substring(0, price.Length - 2);
            var product = new Product()
            {
                Id = model.Id,
                Code = model.Code,
                Name = model.Name,
                Description = model.Description,
                Instruction = model.Instruction,
                Image1 = string.IsNullOrEmpty(model.Image1) ? "" : model.Image1,
                Image2 = model.Image2,
                Image3 = model.Image3,
                Image4 = model.Image4,
                Image5 = model.Image5
            };

            ProductType productType = this.CreateProductType((isCreateNewType != "0"), 
                                                             string.IsNullOrEmpty(ProductType) ? Guid.Empty : Guid.Parse(ProductType), 
                                                             model.ProductTypeCode, 
                                                             model.ProductTypeName);
            ProductPrice productPrice = new ProductPrice()
            {
                Id = Guid.Empty,
                ProductId = model.Id,
                Price = decimal.Parse(price)
            };

            product.ProductType = productType;
            product.ProductPrice = productPrice;

            var fileName = Path.GetFileName(file.FileName);
            var folderName = product.Code;

            var folderPath = Path.Combine(ImageFolder, folderName);
            var serverPath = Server.MapPath(folderPath);
            if (!Directory.Exists(serverPath))
            {
                Directory.CreateDirectory(serverPath);
            }

            var path = Path.Combine(serverPath, fileName);

            file.SaveAs(path);

            product.Image1 = fileName;
            int rs = productBL.CreateProduct(product);

            this.SetProductTypeView();

            ViewBag.Status = rs;
            return View();
        }

        private ProductType CreateProductType(bool isCreateNew, Guid id, string code, string name)
        {
            ProductType productType = new ProductType();

            if (isCreateNew)
            {
                productType.Id = new Guid();
                productType.Code = code;
                productType.Name = name;
            }
            else
            {
                productType.Id = id;
            }

            return productType;
        }
    }
}
